package com.sci.web.controller.gateway.v2;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import com.sci.config.MyConstants;
import com.sci.web.business.common.EmailBusiness;
import com.sci.web.business.gateway.UserBusiness;
import com.sci.web.controller.BaseController;
import com.sci.web.model.genmodel.EmailRecord;
import com.sci.web.model.genmodel.User;
import com.sci.web.util.CommMethod;
import com.sci.web.util.EmailUtil;
import com.sci.web.util.GetWaterId;
import com.sci.web.util.StringUtil;

public class UserController extends BaseController {
	
	
	
	//private userBusiness userBusiness= new userBusiness();
	private UserBusiness userBusiness= new UserBusiness();
	private EmailBusiness emailBusiness= new EmailBusiness();
	
	
	public void login(){
		String name = getPara("name");
		String pwd = getPara("pwd");
		if(StringUtil.isNotEmpty(name) && StringUtil.isNotEmpty(pwd)){
			User user = userBusiness.getUserByNameAndPwd(name, pwd );
			if(user!=null){
				MyConstants.log.info("====>登陆成功........");
				getSession().setAttribute("user", user);
				redirect("/v2/blog/getBlogList");
			}else{
				setAttr("msg", "error");
				render("login.jsp");
			}
		}
	}
	
	
	public void loginout(){
		User user = super.getSessionByUser();
	
		if(user!=null){
			HttpSession session = getRequest().getSession();
			session.removeAttribute("user");
		}
		redirect("/v2");
	}
	
	
	
	
	public void register(){
		String name = getPara("name");
		String pwd = getPara("pwd");
		String email = getPara("email");
		String emailCode = getPara("emailCode");
		
	}

	
	
	
	
	
	/**  
	* @Title: checkEmailUnique  
	* @Description: 注册验证邮箱唯一 
	* @returnType void      
	* @author LiuFei
	* @date 下午11:41:59
	* @throws  
	*/
	public void checkEmailUnique(){
		Map<String , Object> map = new HashMap<String , Object>();
		String email = getPara("email");
		if(StringUtil.isNotEmpty(email)){
			User user = new User();
			user.setUserEmail(email);
			List<User> userByObj = userBusiness.getUserByObj(user);
			if(userByObj.size()>=1){
				map.put("checkVal", false);
			}else{
				map.put("checkVal", true);
			}
			success(map);
		}else{
			error("");
		}
	}
	
	
	
	
	/**  
	* @Title: sendEmailCode  
	* @Description: 发送邮箱验证码
	* @returnType void      
	* @author LiuFei
	* @date 下午11:42:36
	* @throws  
	*/
	public void sendEmailCode(){
		Map<String , Object> map = new HashMap<String , Object>();

		String emailContent = CommMethod.getSysEnumsByEKey("emailCotent").getEValue();
		String emailTitle = CommMethod.getSysEnumsByEKey("emailTitle").getEValue();

		String email = getPara("email");
		if(StringUtil.isNotEmpty(email)){
			
			String valiCode = CommMethod.createValiCode(6);
			
			emailContent = emailContent.replace("_CODE_", valiCode);
			emailContent = emailContent.replace("_HREF_ADDR_", "lexy.ga");
			emailContent = emailContent.replace("_HREF_", "http://lexy.ga");
			System.out.println(emailContent);
			
			EmailUtil.sendEmialCode(email, emailTitle , emailContent);
			EmailRecord emailRecord = new EmailRecord();
			emailRecord.setSerialId(GetWaterId.getWaterId());
			emailRecord.setEmailType("0");
			emailRecord.setSenderEmail("system");
			emailRecord.setRecipientEmail(email);
			emailRecord.setEmailCdoe(valiCode);
			emailRecord.setEmailContent(emailContent);
			int insertEmailRecord = emailBusiness.insertEmailRecord(emailRecord);
			map.put("sendFlag", true);
			success(map);
		}else{
			error("error");
		}
	}
	
	
	
	
	
	
	
	
	
	public void registerUser(){
		
	}
}
